<?php

/***

LZRoster: A roster for World of Warcraft.
Copyright (C) 2007  Emmanuel Cron

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

***/

require_once dirname(__FILE__)."/Sql.class.php";
require_once dirname(__FILE__)."/Log.class.php";
require_once dirname(__FILE__)."/Inputs.class.php";
require_once dirname(__FILE__)."/Client.class.php";
require_once dirname(__FILE__)."/Tooltip.class.php";
require_once dirname(__FILE__)."/SessionRoster.class.php";
require_once dirname(__FILE__)."/Object_.class.php";

class ObjectContainer
{
	static private $instance;
	
	private $items = array();
	private $spells = array();
	
	private $update_max = 500;
	
	private $sql;
	private $log;
	private $client;
	private $tooltip;
	private $session_roster;
	
	private function __construct()
	{
		$this->sql = Sql::GetInstance();
		$this->log = new Log('object_container');
		$this->client = Client::GetInstance();
		$this->tooltip = Tooltip::GetInstance();
		$this->session_roster = SessionRoster::GetInstance();
		
		$this->sql->Query("SET group_concat_max_len = 2000");
	}
	
	static function GetInstance()
	{
		if ( empty(self::$instance) )
		{
			self::$instance = new ObjectContainer();
		}
		
		return self::$instance;
	}
	
	function GetObjects($items, $spells)
	{
		$this->sql->Query("
			SELECT
				GROUP_CONCAT(`Locale`) AS `Locales`,
				GROUP_CONCAT(`Name`) AS `Names`,
				`Color`,
				`ItemId`,
				GROUP_CONCAT(`TooltipOverlib` SEPARATOR '_\$_') AS `Tooltips`,
				`Texture`
			FROM
				`cp_items`
			WHERE
				`ItemId` IN ('".implode("','", $items)."')
			GROUP BY
				`ItemId`
		");
		
		while ( $item = $this->sql->IterateResults())
		{
			$this->items[$item['ItemId']] = new Object_($item);
		}
		
		foreach ($this->items as $item)
		{
			if ($item->HasMissingTooltip())
			{
				$this->session_roster->SetDestination($this->client->GetCurrentPage());
				$this->session_roster->Redirect("update_cache.php");
			}
		}
		
		$this->sql->Query("
			SELECT
				GROUP_CONCAT(`Locale`) AS `Locales`,
				GROUP_CONCAT(`Name`) AS `Names`,
				`Color`,
				`SpellId`,
				GROUP_CONCAT(`TooltipOverlib` SEPARATOR '_\$_') AS `Tooltips`,
				`Texture`
			FROM
				`cp_spells`
			WHERE
				`SpellId` IN ('".implode("','", $spells)."')
			GROUP BY
				`SpellId`
		");
		
		while ( $spell = $this->sql->IterateResults() )
		{
			$this->spells[$spell['SpellId']] = new Object_($spell);
		}
		
		foreach ($this->spells as $spell)
		{
			if ($spell->HasMissingTooltip())
			{
				$spell->session_roster->SetDestination($this->client->GetCurrentPage());
				$spell->session_roster->Redirect("update_cache.php");
			}
		}
	}
	
	function ResetTooltips()
	{
		if ( $this->sql->Query("
			UPDATE
				`cp_items`
			SET
				`TooltipOverlib`=NULL
		") != true)
		{
			return false;
		}
		
		if ( $this->sql->Query("
			UPDATE
				`cp_spells`
			SET
				`TooltipOverlib`=NULL
		") != true)
		{
			return false;
		}
		
		return true;
	}
	
	function UpdateTooltips()
	{
		$inputs = Inputs::GetInstance();
		
		/*** Items ***/
		$this->sql->Query("
			SELECT
				*
			FROM
				`cp_items`
			WHERE
				`TooltipOverlib` IS NULL
			LIMIT
				".$this->update_max."
		");
		
		if ( $this->sql->GetResultsNumber() > 0 )
		{
			$t_results_number = $this->sql->GetResultsNumber();
			
			$update_items_tooltip_query = "
				REPLACE INTO
					`cp_items`
					
					(
					`Revision`,
					`Locale`,
					`ItemId`,
					`Name`,
					`Color`,
					`TooltipLeft`,
					`TooltipRight`,
					`TooltipOverlib`,
					`Texture`
					)
				VALUES
			";
			
			while ( $object_data = $this->sql->IterateResults() )
			{
				$object_tooltip = $this->tooltip->MakeWoWTooltip("i".$object_data['ItemId'], $object_data['Name'], $object_data['Color'], array(
					explode("_\$_", $object_data['TooltipLeft']),
					explode("_\$_", $object_data['TooltipRight'])
				));
				
				$update_items_tooltip_query .= $this->BuildObjectData_Sql($object_data, $object_tooltip);
			}
			$update_items_tooltip_query = substr($update_items_tooltip_query, 0, strlen($update_items_tooltip_query)-1);
			
			if ($this->sql->Query($update_items_tooltip_query) == true)
			{
				$this->log->AddMessage("Updated ".$t_results_number." item(s) tooltip (max: ".$this->update_max.").");
			}
			
			if ($t_results_number == $this->update_max)
			{
				$this->session_roster->Redirect( "update_cache.php?count=".($inputs->Get('count')+1) );
			}
		}
		
		/*** Spells ***/
		$this->sql->Query("
			SELECT
				*
			FROM
				`cp_spells`
			WHERE
				`TooltipOverlib` IS NULL
			LIMIT
				".$this->update_max."
		");
		
		if ( $this->sql->GetResultsNumber() > 0 )
		{
			$t_results_number = $this->sql->GetResultsNumber();
			
			$update_spells_tooltip_query = "
				REPLACE INTO
					`cp_spells`
					
					(
					`Revision`,
					`Locale`,
					`SpellId`,
					`Name`,
					`Color`,
					`TooltipLeft`,
					`TooltipRight`,
					`TooltipOverlib`,
					`Texture`
					)
				VALUES
			";
			
			while ( $object_data = $this->sql->IterateResults() )
			{
				$object_tooltip = $this->tooltip->MakeWoWTooltip("s".$object_data['SpellId'], $object_data['Name'], $object_data['Color'], array(
					explode("_\$_", $object_data['TooltipLeft']),
					explode("_\$_", $object_data['TooltipRight'])
				));
				
				$update_spells_tooltip_query .= $this->BuildObjectData_Sql($object_data, $object_tooltip);
			}
			$update_spells_tooltip_query = substr($update_spells_tooltip_query, 0, strlen($update_spells_tooltip_query)-1);
			
			if ($this->sql->Query($update_spells_tooltip_query) == true)
			{
				$this->log->AddMessage("Updated ".$t_results_number." spell(s) tooltip (max: ".$this->update_max.").");
			}
			
			if ($t_results_number == $this->update_max)
			{
				$this->session_roster->Redirect("update_cache.php");
			}
		}
	}
	
	private function BuildObjectData_Sql ($object_data, $object_tooltip)
	{
		$string = null;
		
		$string .= "(";
		$string .= "'".$object_data['Revision']."'";
		$string .= ",";
		$string .= "'".$object_data['Locale']."'";
		$string .= ",";
		$string .= "'".( array_key_exists('ItemId', $object_data) ? $object_data['ItemId'] : $object_data['SpellId'] )."'";
		$string .= ",";
		$string .= "'".addslashes($object_data['Name'])."'";
		$string .= ",";
		$string .= "'".$object_data['Color']."'";
		$string .= ",";
		$string .= "'".addslashes($object_data['TooltipLeft'])."'";
		$string .= ",";
		$string .= "'".addslashes($object_data['TooltipRight'])."'";
		$string .= ",";
		$string .= "'".addslashes($object_tooltip)."'";
		$string .= ",";
		$string .= "'".addslashes($object_data['Texture'])."'";
		$string .= "),";
		
		return $string;
	}
	
	function GetItem($item)
	{
		if ( array_key_exists($item, $this->items) )
		{
			return $this->items[$item];
		}
		else
		{
			return null;
		}
	}
	
	function GetSpell($spell)
	{
		if ( array_key_exists($spell, $this->spells) )
		{
			return $this->spells[$spell];
		}
		else
		{
			return null;
		}
	}
}

?>